package net.toocruel.iqismart.entity.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import net.toocruel.iqismart.entity.base.BaseEntity;
import net.toocruel.iqismart.entity.core.Attention;
import net.toocruel.iqismart.entity.rbac.User;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.persistence.*;
import java.util.Set;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/8 上午11:35
 */
@Entity
public class Tag extends BaseEntity {
    private static final long serialVersionUID = 2528353191493076122L;
    private String name;
    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY)
    @JoinColumn(name = "categoryId")
    @JsonIgnore
    private Category category;
    @Transient
    private Long categoryId;
    private String logo;
    private String summary;
    private String description;
    @ManyToOne(cascade = CascadeType.DETACH)
    @JoinColumn(name = "parentId")
    @JsonIgnore
    private Tag parent;
    private int followers;
    private int questions;

    @OneToMany(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,mappedBy = "tag")
    private Set<Attention> attentions;

    @Transient
    private Boolean attented = false;

    public Boolean getAttented() {
        User currentUser = null;
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if(principal instanceof User){
                 currentUser = (User) principal;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(currentUser == null){
            return attented;
        }
        if(CollectionUtils.isNotEmpty(attentions)){
            User finalCurrentUser = currentUser;
            attentions.forEach(attention -> {
                if(attention.getUser().getId() == finalCurrentUser.getId()){
                    attented = true;
                }
            });
        }

        return attented;
    }

    public void setAttented(Boolean attented) {
        this.attented = attented;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public int getQuestions() {
        return questions;
    }

    public void setQuestions(int questions) {
        this.questions = questions;
    }

    public Tag() {
    }

    public Tag(String name) {
        this.name = name;
    }

    public Long getCategoryId() {
        if(categoryId!=null && categoryId>0){
            return categoryId;
        }
        if(category!=null){
            return category.getId();
        }
        return categoryId;
    }

    public Set<Attention> getAttentions() {
        return attentions;
    }

    public void setAttentions(Set<Attention> attentions) {
        this.attentions = attentions;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Tag getParent() {
        return parent;
    }

    public void setParent(Tag parent) {
        this.parent = parent;
    }

    public int getFollowers() {
        return followers;
    }

    public void setFollowers(int followers) {
        this.followers = followers;
    }


}
